//
//  AddressSelectView.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/26.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "AddressSelectView.h"

@interface AddressSelectView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UIView *containView;/**< 容器view*/
@property (nonatomic, strong) UILabel *titleLabel;/**< 显示的名字*/
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, copy) completeBlock block;
@property (nonatomic, strong) AddressListModel *selectModel;
@property (nonatomic, copy) NSDictionary *dataDic;
@property (nonatomic, assign) NSInteger selectProviceRow;
@property (nonatomic, assign) NSInteger selectCityRow;
@property (nonatomic, assign) NSInteger selectDistrictRow;
@end

@implementation AddressSelectView

+(void)showPickerWithDataDic:(NSDictionary *)dataDic SelectModel:(AddressListModel *)selectModel complete:(completeBlock)complete{
    AddressSelectView *picker= [[AddressSelectView alloc] init];
    picker.selectModel = selectModel;
    picker.dataDic = dataDic;
    picker.titleLabel.text = @"所在地区";
    picker.block = complete;
    [[UIApplication sharedApplication].keyWindow addSubview:picker];
    [picker showAction];
    
    if (selectModel.provinceName.length == 0) {//说明没选过
        [picker.pickerView selectRow:0 inComponent:0 animated:NO];
        [picker.pickerView selectRow:0 inComponent:1 animated:NO];
        [picker.pickerView selectRow:0 inComponent:2 animated:NO];
        picker.selectProviceRow = 0;
        picker.selectCityRow = 0;
        picker.selectDistrictRow = 0;
    }else{//找到上次选择
        //全部省的数据
        NSArray *allProviceArray = dataDic.allKeys;
        for (int i = 0; i < allProviceArray.count; i++) {//寻找省
            NSString *proviceString = allProviceArray[i];
            if (proviceString == selectModel.provinceName) {
                [picker.pickerView selectRow:i inComponent:0 animated:NO];
                picker.selectProviceRow = i;
                [picker.pickerView reloadComponent:0];
                //当前省下所有市
                NSArray *allCityArray = [dataDic[proviceString] allKeys];
                for (int j = 0; j < allCityArray.count; j++) {//寻找市
                    NSString *cityString = allCityArray[j];
                    if (cityString == selectModel.cityName) {
                        [picker.pickerView selectRow:j inComponent:1 animated:NO];
                        picker.selectCityRow = j;
                        [picker.pickerView reloadComponent:1];
                        //判断是否有区级别
                        if (selectModel.districtName.length > 0) {
                            NSArray *districtArray = dataDic[proviceString][cityString];
                            for (int k = 0; k < districtArray.count; k++) {//寻找区
                                NSString *regionString = districtArray[k];
                                if (selectModel.districtName == regionString) {
                                    NSLog(@"选中的区是 %@",regionString);
                                    picker.selectDistrictRow = k;
                                    [picker.pickerView selectRow:k inComponent:2 animated:NO];
                                    [picker.pickerView reloadComponent:2];
                                    break;
                                }
                            }
                        }else{
                            [picker.pickerView selectRow:0 inComponent:2 animated:NO];
                            [picker.pickerView reloadComponent:2];
                        }
                    }
                }
            }
        }
    }
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UIView *containView = [[UIView alloc] init];
    containView.frame = CGRectMake(0,SCREEN_HEIGHT,SCREEN_WIDTH, kAppWidth_6(250));
    [self addSubview:containView];
    [containView setlayerCornerRadius:5 corners:UIRectCornerTopLeft|UIRectCornerTopRight];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancleAction)];
    [self addGestureRecognizer:tap];
    tap = [[UITapGestureRecognizer alloc] init];
    [containView addGestureRecognizer:tap];
    self.containView = containView;
    
    UIView *toolBar = [[UIView alloc] init];
    toolBar.frame = CGRectMake(0, 0,SCREEN_WIDTH,kAppWidth_6(35));
    toolBar.backgroundColor = UICOLOR_HEX(0xffffff);
    [containView addSubview:toolBar];
    
    [toolBar addLineWithFrame:CGRectMake(0,toolBar.xl_height-0.5,SCREEN_WIDTH, 0.5) color:[UIColor lightGrayColor]];
  
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kAppWidth_6(50), 0, SCREEN_WIDTH - kAppWidth_6(100),toolBar.xl_height)];
    titleLabel.font = UIPFRegularFont(15);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = UICOLOR_HEX(0x333333);
    [containView addSubview:titleLabel];
    self.titleLabel=titleLabel;
    
    UIButton *cancleButton = [UIButton initWithTitleStr:@"取消" titleColor:[UIColor blackColor] withFont:UIPFRegularFont(13)];
    cancleButton.frame = CGRectMake(0, 0, kAppWidth_6(50), toolBar.xl_height);
    [cancleButton addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    [toolBar addSubview:cancleButton];
    
    UIButton *sureButton = [UIButton initWithTitleStr:@"确定" titleColor:[UIColor blackColor] withFont:UIPFRegularFont(13)];
    sureButton.frame = CGRectMake(SCREEN_WIDTH - kAppWidth_6(50), 0, kAppWidth_6(50), toolBar.xl_height);
    [sureButton addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [toolBar addSubview:sureButton];
    
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.backgroundColor = UICOLOR_HEX(0xffffff);
    pickerView.frame = CGRectMake(0, toolBar.xl_bottom, SCREEN_WIDTH, containView.xl_height - toolBar.xl_height);
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [containView addSubview:pickerView];
    self.pickerView = pickerView;
}

#pragma mark - UIPickerDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.dataDic.allKeys.count;
    }else if(component == 1){
        NSString *provincString = self.dataDic.allKeys[self.selectProviceRow];
        return [self.dataDic[provincString] allKeys].count;
    }else{
        NSString *provincString = self.dataDic.allKeys[self.selectProviceRow];
        NSString *cityString = [self.dataDic[provincString] allKeys][self.selectCityRow];
        return [self.dataDic[provincString][cityString]count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        NSString *provincString = self.dataDic.allKeys[row];
        return provincString;
    }else if(component == 1){
        NSString *provincString = self.dataDic.allKeys[self.selectProviceRow];
        NSString *cityString = [self.dataDic[provincString] allKeys][row];
        return cityString;
    }else{
        NSString *provincString = self.dataDic.allKeys[self.selectProviceRow];
        NSString *cityString = [self.dataDic[provincString] allKeys][self.selectCityRow];
        NSArray *dicArray = self.dataDic[provincString][cityString];
        return dicArray[row];
    }
}

// 选中某一组中的某一行时调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectProviceRow = row;
        self.selectCityRow = 0;
        self.selectDistrictRow = 0;
        [_pickerView selectRow: 0 inComponent: 1 animated: NO];
        [_pickerView selectRow: 0 inComponent: 2 animated: NO];
        [_pickerView reloadComponent: 1];
        [_pickerView reloadComponent: 2];
    }else if(component == 1){
        self.selectCityRow = row;
        self.selectDistrictRow = 0;
        [_pickerView selectRow: 0 inComponent: 2 animated: NO];
        [_pickerView reloadComponent: 2];
    }else{
        self.selectDistrictRow = row;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    //设置分割线的颜色
//    for(UIView *singleLine in pickerView.subviews){
//        if (singleLine.frame.size.height < 1){
//            singleLine.backgroundColor = [UIColor blackColor];
//        }
//    }
//
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = UIPFRegularFont(18);
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.minimumScaleFactor = 0.5;
        pickerLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

#pragma mark - 取消
-(void)cancleAction{
    [UIView animateWithDuration:0.25 animations:^{
        self.containView.xl_y = SCREEN_HEIGHT;
        self.backgroundColor = [UIColor clearColor];
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)showAction{
    [UIView animateWithDuration:0.25 animations:^{
        self.containView.xl_y = SCREEN_HEIGHT - self.containView.xl_height;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    }];
}

#pragma mark - 确定
-(void)sureAction{
    NSString *provincString = self.dataDic.allKeys[self.selectProviceRow];
    NSString *cityString = [self.dataDic[provincString] allKeys][self.selectCityRow];
    NSArray *dicArray = self.dataDic[provincString][cityString];
    NSString *districtString = @"";
    if (dicArray.count > 0) {
         districtString = dicArray[self.selectDistrictRow];
    }

    self.selectModel.provinceName = provincString;
    self.selectModel.cityName = cityString;
    self.selectModel.districtName = districtString;
    
    if (self.block) {
        self.block();
    }
    [self cancleAction];
}


@end
